package com.ai3d.rfid.network.interceptor

import com.ai3d.rfid.base.app.App.Companion.appViewModel
import com.ai3d.rfid.common.libs.InitLanguage
import com.ai3d.rfid.network.bean.ApiResponse
import com.bj.disc.ui.login.LRConstants
import com.bj.disc.ui.login.activity.LoginActivity
import com.bj.disc.ui.login.activity.RegisterActivity
import com.bj.disc.ui.splash.activity.SplashActivity
import com.google.gson.Gson
import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody
import java.io.IOException

/**
 * expired token interceptor
 */
class TokenOutInterceptor : Interceptor {

    val gson: Gson by lazy { Gson() }

    @kotlin.jvm.Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val response = chain.proceed(chain.request())
        return if (response.body() != null && response.body()!!.contentType() != null) {
            val mediaType = response.body()!!.contentType()
            val string = response.body()!!.string()
            val responseBody = ResponseBody.create(mediaType, string)
            try {//处理服务器关闭导致crash问题
                val apiResponse = gson.fromJson(string, ApiResponse::class.java)
                //判断逻辑 模拟一下
                if (apiResponse.code == 99999) {
                    //如果是普通的activity话 可以直接跳转，如果是navigation中的fragment，可以发送通知跳转
                    /*TODO appContext.startActivity(Intent(appContext, TestActivity::class.java).apply {
                         flags = Intent.FLAG_ACTIVITY_NEW_TASK
                     })*/
                }
                when (apiResponse.code) {
                    LRConstants.LOGIN_EXPIRED, LRConstants.LOGIN_HAT_EXPIRED -> {
                        try {
                            //过滤掉SplashFragment和LoginFragment
                            if ((InitLanguage.activityList.size == 1 || InitLanguage.activityList.size == 2)
                                && (InitLanguage.activityList[InitLanguage.activityList.size - 1] is SplashActivity
                                        || InitLanguage.activityList[InitLanguage.activityList.size - 1] is LoginActivity
                                        || InitLanguage.activityList[InitLanguage.activityList.size - 1] is RegisterActivity
                                        )
                            ) {
//                                HandlerUtils.postDelayed({ ToastProxy.showLong(R.string.account_past_due_tips) },100)
                            } else {
                                appViewModel.loginExpired.postValue(true)
                            }
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
                response.newBuilder().body(responseBody).build()
            } catch (e: Exception) {
                e.printStackTrace()
                response.newBuilder().body(responseBody).build()
            }
        } else {
            response
        }
    }
}